package com.example.service.impl;

import com.example.model.Student;
import com.example.repository.es.StudentEsRepository;
import com.example.service.StudentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * 学生服务实现类
 * 仅操作Elasticsearch
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class StudentServiceImpl implements StudentService {
    
    private final StudentEsRepository elasticsearchRepository;
    
    /**
     * 保存学生信息
     */
    @Override
    public Student saveStudent(Student student) {
        log.info("保存学生信息: {}", student.getName());
        
        // 保存到Elasticsearch
        Student esStudent = elasticsearchRepository.save(student);
        log.info("学生信息已保存到Elasticsearch: {}", esStudent.getId());
        
        return esStudent;
    }
    
    /**
     * 根据ID查找学生
     */
    @Override
    public Optional<Student> findById(String id) {
        log.info("根据ID查找学生: {}", id);
        
        // 从Elasticsearch查询
        Optional<Student> esStudent = elasticsearchRepository.findById(id);
        if (esStudent.isPresent()) {
            log.info("从Elasticsearch找到学生: {}", esStudent.get().getName());
        }
        
        return esStudent;
    }
    
    /**
     * 获取所有学生（分页）
     */
    @Override
    public Page<Student> findAllStudents(Pageable pageable) {
        log.info("获取所有学生，页码: {}, 大小: {}", pageable.getPageNumber(), pageable.getPageSize());
        return elasticsearchRepository.findAll(pageable);
    }
    
    /**
     * 根据姓名搜索学生（使用ES全文搜索）
     */
    @Override
    public List<Student> searchByName(String name) {
        log.info("根据姓名搜索学生: {}", name);
        return elasticsearchRepository.findByName(name);
    }
    
    /**
     * 根据年龄范围查找学生
     */
    @Override
    public List<Student> findByAgeRange(Integer minAge, Integer maxAge) {
        log.info("根据年龄范围查找学生: {} - {}", minAge, maxAge);
        return elasticsearchRepository.findByAgeBetween(minAge, maxAge);
    }
    
    /**
     * 根据学号查找学生
     */
    @Override
    public Student findByNumber(String number) {
        log.info("根据学号查找学生: {}", number);
        return elasticsearchRepository.findByNumber(number);
    }
    
    /**
     * 根据父亲姓名查找学生
     */
    @Override
    public List<Student> findByFatherName(String fatherName) {
        log.info("根据父亲姓名查找学生: {}", fatherName);
        return elasticsearchRepository.findByParentFather(fatherName);
    }
    
    /**
     * 根据母亲姓名查找学生
     */
    @Override
    public List<Student> findByMotherName(String motherName) {
        log.info("根据母亲姓名查找学生: {}", motherName);
        return elasticsearchRepository.findByParentMother(motherName);
    }
    
    /**
     * 更新学生信息
     */
    @Override
    public Student updateStudent(Student student) {
        log.info("更新学生信息: {}", student.getId());
        
        // 更新Elasticsearch
        Student esStudent = elasticsearchRepository.save(student);
        
        log.info("学生信息更新完成: {}", student.getName());
        return esStudent;
    }
    
    /**
     * 删除学生信息
     */
    @Override
    public void deleteStudent(String id) {
        log.info("删除学生信息: {}", id);
        
        // 从Elasticsearch删除
        elasticsearchRepository.deleteById(id);
        log.info("已从Elasticsearch删除学生: {}", id);
    }
    
    /**
     * 统计学生总数
     */
    @Override
    public long countStudents() {
        return elasticsearchRepository.count();
    }
}